<?php
/**
 * This file is part of DomBankLink.
 * 
 * DomBankLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomBankLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomBankLink.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
 * @author Oliver Leisalu
 * @package DomBankLink
 */
abstract class DomBankLink {
	
	protected $profileName;
	
	protected $defaultCallbackUrl;
	
	protected $currency = 'EEK';
	
	private static $links;
	
	const CONF_FILE_NAME = 'banklink.conf.php';
	
	
	public static function c( $profileName = NULL ) {
		$cl = get_called_class();
		return new $cl( $profileName );
	}
	
	/**
	 * @param String $profileName
	 */
	public function __construct( $profileName = NULL ) {
		if( $profileName == NULL )
			$profileName = $this->getKey();
		$this->profileName = $profileName;
		self::$links[strtolower($profileName)] = $this;
	}
	/**
	 * @return String
	 */
	public function getProfileName() {
		return $this->profileName;
	}
	/**
	 * @return String
	 */
	public function getCurrency() {
		return $this->currency;
	}
	/**
	 * Returns bank name
	 * @return String
	 */
	abstract public function getName();
	/**
	 * Returns bank key. For example seb, swed etc
	 * @return String
	 */
	abstract public function getKey();
	/**
	 * Should return 88x31px icon url.
	 * @return String
	 */
	abstract public function getIconUrl();
	
	/**
	 * @param DomBankLinkObject $payment
	 */
	abstract public function sendToBank( DomBankLinkObject $payment );
	/**
	 * Handles replay from bank
	 *
	 * @param String $paymentClassName
	 * @param Int $requestId Only needed for identify requests. Payments should get ID from request itself.
	 */
	abstract public function handleReplay( $paymentClassName, $requestId = NULL );
	
	/**
	 * Loads configuration file
	 */
	public static function loadLinks() {
		if( self::$links == NULL ) {
			$configFilePath = DOM_CONF_DIR_PATH . CONF_FILE_NAME;
			
			if( !file_exists( $configFilePath ) )
				trigger_error('Config file is not found. Expected path: <strong>'.$configFilePath.'</strong>', E_USER_ERROR );
				
			require( $configFilePath );
		}
	}
	/**
	 * @param String $profile
	 * @return DomBankLink
	 */
	public static function getLink( $profile ) {
		self::loadLinks();
		return self::$links[strtolower($profile)];
	}
	/**
	 * Returns all usable links
	 * @return Array
	 */
	public static function getLinks() {
		return self::$links;
	}
	/**
	 * Returns link callback url
	 *
	 * @param DomBankPayment $payment
	 * @return String
	 */
	public function getCallBackUrl( DomBankLinkObject $payment ) {
		if( $payment instanceof DomBankIdentify )
			$extra .= $payment->id.'/';
		else
			$extra = '';
		return ($payment->callBackUrl ? $payment->callbackUrl : $this->defaultCallbackUrl) . $extra;
	}
	
	/**
	 * @param String $method
	 * @param array $arguments
	 * @return DomBankLink
	 */
	public function __call( $method, array $arguments) {
		$r = property_exists( get_class($this), $method);
		if (!$r) {
			$x = new ReflectionClass(get_class($this));
			$r = $x->hasProperty($method);
		}
		
		if( $r ) {
			$this->$method = $arguments[0];
		} else {
			throw new  RuntimeException('Called to non existing method '.$method.'!');
		}
		return $this;
	}
	
	
}

?>